package com.gaohe.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaohe.common.R;
import com.gaohe.pojo.Shop;
import com.gaohe.service.ShopService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
@Slf4j
public class ShopController {
    @Autowired
    private ShopService shopService;
    @GetMapping("/page")
    public R page(@RequestParam int page,
                  @RequestParam int pageSize,
                  @RequestParam(required = false) String name){
        Page<Shop> shopPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Shop> qw = new LambdaQueryWrapper<>();
        qw.like(!StringUtils.isEmpty(name),Shop::getShopName,name);
        shopService.page(shopPage,qw);
        return R.success(shopPage);
    }
    //修改商品
    //回显
    @GetMapping("/{id}")
    public R findByid(@PathVariable Long id){
        return R.success(shopService.getById(id));
    }
    @PutMapping
    public R update(@RequestBody Shop shop)
    {
        boolean b = shopService.updateById(shop);
        return b?R.success("修改成功"):R.error("修改失败");
    }
    @PostMapping
    public R save(@RequestBody Shop shop)
    {
        boolean save = shopService.save(shop);
        return save?R.success("新增成功"):R.error("新增失败");
    }
    @DeleteMapping
    public R delete(@RequestParam Long id){
        boolean b = shopService.removeById(id);
        return b?R.success("删除成功"):R.error("删除失败");
    }
    @GetMapping("/getShopByCategoryId/{id}")
    public R getShopByCategoryId(@PathVariable Long id){
        LambdaQueryWrapper<Shop> qw = new LambdaQueryWrapper<>();
        qw.eq(Shop::getCategoryId,id);
        List<Shop> list = shopService.list(qw);
        return R.success(list);
    }

}
